package com.liusy.code;

import java.io.File;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;

/**
 * 项目信息
 * 
 * @author liusy
 *
 */
public class ProjectBean
{
	private String name;
	private String root;
	private String src;
	private String webRoot;
	private String webRootName;
	private String webInf;
	private String lib;
	private IProject project;
	private IJavaProject javaProject;

	/**
	 * 
	 * @param selection
	 */
	public ProjectBean(ISelection selection)
	{

		
//		 if (selection instanceof StructuredSelection) {
//			   Object obj = ((StructuredSelection) selection).getFirstElement();
//			   String path = null;
//			   if (obj instanceof IResource) {
//			    IResource iRes = ((IResource) obj);
//			    if (iRes.getFileExtension() == null) {
//			     path = iRes.getLocation().makeAbsolute().toFile().getPath();
//			    }else{
//			     path = iRes.getLocation().makeAbsolute().toFile().getParent();
//			    }
//			   } else if (obj instanceof IJavaElement) {
//			    IResource ijavaEle = ((IJavaElement) obj).getResource();
//			    if (ijavaEle.getFileExtension() == null) {
//			     path = ijavaEle.getLocation().makeAbsolute().toFile().getPath();
//			    } else {
//			     path = ijavaEle.getLocation().makeAbsolute().toFile().getParent();
//			    }
//			   } else {
//			    System.out.println(obj.getClass());
//			   }
//			   if (path != null) {
//			    Runtime run = Runtime.getRuntime();
//			    try {
//			     run.exec("rundll32 SHELL32.DLL,ShellExec_RunDLL  " + path);
//			    } catch (IOException e) {
//			     e.printStackTrace();
//			    }
//			   }
//			  }
			//取得项目根目录
			Object obj = ((IStructuredSelection) selection).getFirstElement();
		 
		IJavaElement t = (IJavaElement) obj;
		javaProject = t.getJavaProject();
		project = t.getResource().getProject();
		IClasspathEntry [] classpath= null;
		try {
			classpath = javaProject.getRawClasspath();
		} catch (JavaModelException e) {
			e.printStackTrace();
		}
		if(null!=classpath && classpath.length>0)
		{
			src=classpath[0].getPath().toString();
		}
		IPackageFragmentRoot pkroot = javaProject.getPackageFragmentRoot(javaProject.getResource());
		System.out.println(pkroot.getElementName());
		
		//项目名称
		name = project.getName();
		root = project.getLocation().toString();
		//取得源目录
		//src = root + "/src";
		//取得WEB-INF目录
		webInf = root+"\\web".replaceAll("\\\\", "/");
		//取得webRoot目录
		webRootName = getWebRootName(webInf);
		webRoot = root + "/" + getWebRootName(webInf);
		//取得lib目录
		lib = webInf + "/lib";
	}

	/**
	 * 取得WEB-INF路径
	 * @param root
	 * @return
	 */
	private String getWebInf(String root)
	{
		String webinfo = null;
		File f = new File(root);
		if (f.isDirectory())
		{
			File[] files = f.listFiles();
			for (File file : files)
			{
				if (file.isDirectory())
				{
					if (file.getName().equals("WEB-INF"))
					{
						webinfo = file.getPath();
						return webinfo;
					} else
					{
						webinfo = getWebInf(file.getPath());
					}
				}
			}
		}
		return webinfo;
	}

	/**
	 * 取得WebRoot的名称
	 * @param Projectpath
	 * @return "WebRoot"
	 */
	private String getWebRootName(String webInf)
	{
		return webInf.split("/")[(webInf.split("/").length - 2)];
	}

	/**
	 * 刷新工程
	 */
	public void refresh()
	{
		try
		{
			project.refreshLocal(IResource.DEPTH_INFINITE, null);
		} catch (CoreException e)
		{
			e.printStackTrace();
		}
	}

	public String getRoot()
	{
		return root;
	}

	public void setRoot(String root)
	{
		this.root = root;
	}

	public String getSrc()
	{
		return src;
	}

	public void setSrc(String src)
	{
		this.src = src;
	}

	public String getWebRoot()
	{
		return webRoot;
	}

	public void setWebRoot(String webRoot)
	{
		this.webRoot = webRoot;
	}

	public String getWebInf()
	{
		return webInf;
	}

	public void setWebInf(String webInf)
	{
		this.webInf = webInf;
	}

	public String getLib()
	{
		return lib;
	}

	public void setLib(String lib)
	{
		this.lib = lib;
	}

	public IProject getProject()
	{
		return project;
	}

	public void setProject(IProject project)
	{
		this.project = project;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public IJavaProject getJavaProject()
	{
		return javaProject;
	}

	public void setJavaProject(IJavaProject javaProject)
	{
		this.javaProject = javaProject;
	}

	public String getWebRootName()
	{
		return webRootName;
	}
}
